{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Huggingface平台（国外的模型库）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 设置自己的token\n",
    "1. 点击头像\n",
    "2. 点击Access Tokens\n",
    "3. create new token创建新token（能点全选）\n",
    "4. 保存token （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）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 安装huggingface库\n",
    "pip install transformers datesets tokenizers\n",
    "\n",
    "#### 调用线上的模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_URL = \"Huggingface上模型路径，一般以https://api-interence.huggingface.co/models/开头\"\n",
    "API_TOKEN = \"Huggingface上token\"\n",
    "headers = {\"Authorization\": f\"Bearer {API_TOKEN}\"}\n",
    "\n",
    "response = requests.post(API_URL, headers=headers,json={\"inputs\": \"Hello, my name is\"})\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 调用本地模型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "from transformer import AutoModelForCausalLM, AutoTokenizer,pipline\n",
    "# 下载模型\n",
    "model_name = \"模型路径\"\n",
    "cache_dir = \"缓存路径\"\n",
    "\n",
    "model = AutoModelForCausalLM.from_pretrained(model_name, cache_dir=cache_dir)\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)\n",
    "\n",
    "print(f\"模型路径：{cache_dir}\")\n",
    "\n",
    "# 生成器\n",
    "generator = pipline(\"text-generation\", model=model, cache_dir=cache_dir,device=\"cpu\")\n",
    "\n",
    "ouput =  generator(\"你好\", max_lentgh=50,num_return_sequences=1)\n",
    "\n",
    "print(ouput)\n",
    "\n",
    "# 编码器\n",
    "classifier = pipline(\"text-classification\", model=model, cache_dir=cache_dir,device=\"cpu\")\n",
    "\n",
    "result = classifier(\"你好\")\n",
    "\n",
    "print(result)\n",
    "\n",
    "#更多方法可以点击pipline查看文档\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
